package com.wwz.path;

import com.wwz.annoation.Path;
import com.wwz.utils.ClassUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.messaging.handler.HandlerMethod;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.handler.AbstractHandlerMethodMapping;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.*;

/**
 * @author wwz
 */
@Component
public class PathMappingCreator extends AbstractHandlerMethodMapping{


    private String basePackage;
    private static Map<String,Method> urlMap = new HashMap<String,Method>();
    private static Map<String,Class<?>> beanMap = new HashMap<String,Class<?>>();


    private PathMappingCreator pathMappingCreator;

    @Override
    protected boolean isHandler(Class aClass) {
        return false;
    }

    @Nullable
    @Override
    protected Object getMappingForMethod(Method method, Class aClass) {
        return null;
    }

    @Override
    protected Set<String> getMappingPathPatterns(Object o) {
        return null;
    }

    @Nullable
    @Override
    protected Object getMatchingMapping(Object o, HttpServletRequest httpServletRequest) {
        return null;
    }

    @Override
    protected Comparator getMappingComparator(HttpServletRequest httpServletRequest) {
        return null;
    }

   /* public PathMappingCreator(String urlPath,Object handler,Method method){
        registerMapping(urlPath,handler,method);
    }*/
    public  static class Creator{
        public static void init(String basePackage){
            try {
                Set<Class<?>> set =  ClassUtils.getClasses(basePackage);
                Iterator iterator = set.iterator();
                while (iterator.hasNext()){
                    Class<?> c =  (Class<?>)iterator.next();
                    if( c.isAnnotationPresent(Controller.class)){
                        Method[] methods = c.getDeclaredMethods();
                        for(Method method:methods){
                            Path path =  method.getAnnotation(Path.class);
                            if(path!=null){
                                RequestMapping requestMapping = c.getAnnotation(RequestMapping.class);
                                if(requestMapping!=null){
                                    for(String url:requestMapping.value()){
                                        for(String p:path.value()){
                                            String urlPath = "/".concat(url).concat("/").concat(p).replace("//","/");
                                            if(urlPath.lastIndexOf("/")>0){
                                                urlPath = urlPath.substring(0,urlPath.length());
                                            }
                                            urlMap.put(urlPath,method);
                                            beanMap.put(urlPath,c);
                                        }
                                    }
                                }

                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        private static String arrayConvertStr(String[] s){
            return Arrays.asList(s).toString();
        }
    }




    public static Map<String,Method> getPathMethod(){
        return urlMap;
    }

    public static void main(String[] args){
        PathMappingCreator.Creator.init("com.wwz.controller");
    }

    public String getBasePackage() {
        return basePackage;
    }

    public void setBasePackage(String basePackage) {
        this.basePackage = basePackage;
    }
}
